//public variables
var contactList = new Array();

//public visual controlls
var contactGrid;
var messageTabs;
var extensionWindow;
var combo;

//ContactList - Constants
var STATUS = 0;
var NAME = 1;
var MESSAGE_STACK_SIZE = 2;
var MESSAGE_STACK = 3;

//MessageStack - Constants
var SENDER = 0;
var TIME = 1;
var MESSAGE = 2;

function onContactClick( grid, rowIndex, e)
{
	displayContactTab(contactList[rowIndex][NAME]);
}

function onTabChange( tabpanel, tab )
{
	//TODO: Tabelle neu markieren
	var index = getContactListIndex(tab.title);
	if (index == -1) return;
	displayContactTab(tab.title);
}

function displayContactTab(name)
{
	var tabIndex = getMessageTabsIndex(name);
	var contactListIndex = getContactListIndex(name);
	if (tabIndex == -1)
	{
		var tabMainDiv = "<div id='tabDiv_" + contactList[contactListIndex][NAME] + "' />"; 
		messageTabs.add({closable:true,title: contactList[contactListIndex][NAME],html: tabMainDiv});
		messageTabs.setActiveTab(messageTabs.items.getCount() -1);
	}
	else
	{
		messageTabs.setActiveTab(tabIndex);
	}
	refreshTabBody(contactListIndex);
}

function refreshTabBody(index)
{
	var tabMainDiv = document.getElementById("tabDiv_" + contactList[index][NAME]);
	var stack = contactList[index][MESSAGE_STACK];
	var tabBody = "";
	for (var msgIndex = 0 ; msgIndex < stack.length ; msgIndex++)
	{
		var color = stack[msgIndex][SENDER] == ownUserName ? "red" : "blue";
		tabBody = tabBody + 
			"<font color='" + color + "'>" + stack[msgIndex][SENDER] + "</font> (" + 
			stack[msgIndex][TIME] + ")<br>" + 
			stack[msgIndex][MESSAGE]+"<p>";
	}
	while (tabBody.indexOf(":-)") != -1)
		tabBody = tabBody.replace(":-)", "<img src='images/Smile.png' />")
	tabMainDiv.innerHTML = tabBody;
}

function messageHandler(result, request)
{
	var queue = Ext.util.JSON.decode(result.responseText);
	processQueue(queue);
	setTimeout("getMessages()", 1000);
}

function processQueue(queue)
{
	for (var itemIndex = 0 ; itemIndex < queue.items.length ; itemIndex++)
	{
		var item = queue.items[itemIndex];
		switch (item.type)
		{
		case "CONTACTS":
			updateContactList(item.contacts);
			break;
		case "ACTIVATION":
			updateContactStatus(item.name, item.isActive);			
			break;
		case "TEXT":
			putTextMessageOnStack(item.sender, item.receiver, item.message);
			break;
		case "INFO":
			Ext.Msg.alert(item.title, item.message);
		}
	}
}

function updateContactList(contacts)
{
	var newContactList = new Array();
	for (var contactIndex = 0 ; contactIndex < contacts.length ; contactIndex++)
	{
		var contact = contacts[contactIndex];
		var index = getContactListIndex(contact.name);
		if (index == -1)
		{
			newContactList.push(['-',contact.name, 0, getNewMessageStack()]);
		}
		else
		{
			newContactList.push(contactList[index]);
		}
	}
	contactList = newContactList;
	contactGrid.getStore().loadData(contactList);
	contactGrid.getView().refresh();
	for (var tabIndex = 1 ; tabIndex < messageTabs.items.length ; tabIndex++)
	{
		var tab = messageTabs.get(tabIndex);
		var isAvailable = false;
		for (var rowIndex = 0 ; rowIndex < contactList.length ; rowIndex++)
		{
			if (tab.title == contactList[rowIndex][NAME])
			{
				isAvailable = true;
				break;
			}
		}
		if (!isAvailable) messageTabs.remove(tabIndex);
	}
}

function updateContactStatus(name, isActive)
{
	var index = getContactListIndex(name);
	if (index == -1) return;
	contactList[index][STATUS] = isActive ? "+" : "-";
	contactGrid.getStore().loadData(contactList);
	contactGrid.getView().refresh();
}

function putTextMessageOnStack(sender, receiver, message)
{
	var index = getContactListIndex(sender == ownUserName ? receiver : sender);
	if (index == -1) return;
	var stack = contactList[index][MESSAGE_STACK];
	var msgTime = (new Date()).getHours() + ":" + (new Date()).getMinutes();
	stack.push([sender, msgTime, message]);
	if (messageTabs.getActiveTab().title == contactList[index][NAME])
		displayContactTab(contactList[index][NAME]);
}

function getNewMessageStack()
{
	return new Array();
}

function getContactListIndex(name)
{
	for (var contactIndex = 0 ; contactIndex < contactList.length ; contactIndex++)
	{
		if (contactList[contactIndex][NAME] == name)
		{
			return contactIndex;
		}
	}
	return -1;
}

function getMessageTabsIndex(name)
{
	for (var tabIndex = 1 ; tabIndex < messageTabs.items.length ; tabIndex++)
	{
		var tab = messageTabs.get(tabIndex);
		if (tab.title == name)
		{
			return tabIndex;
		}
	}
	return -1;
}